import { App, epclass, toast, navigate } from '../../library/api/index'

Page({
  data: {
    moreReviews: [],
    reviews: [],
    lives: [],
    tip: 'blank',
    hide_review: false,
    tab_live: '',
    tab_review: 'on',
    navIndex: 'on',
    hide_more_class: true,
    no_live_class: true,
    is_load_classes: false
  },
  onLoad: function(e) {
    App.jice.onLoad(e)
  },
  onReady: function() {
    // 等待整个App登录和授权并且服务器创建成功
    let waiter = setInterval(() => {
      if(App.uid()) {
        this.getLiveClasses()
        this.getReviewClasses()
        clearInterval(waiter)
        this.setData({
          is_load_classes: true
        })
      }
    }, 200)
    App.jice.track('page', {
      pagepath: '/pages/index/index',
      pageTitle: '首页',
      pageType: 'enter'
    })
    App.logger({
      type: App.G.view,
      channel: App.G.channel,
      target: '首页'
    })
  },
  onShow: function() {
    if(this.data.is_load_classes) {
      this.setData({
        reviews: [],
        lives: []
      })
      this.getLiveClasses()
      this.getReviewClasses()
    }
    App.jice.track('appshow')
  },
  showWaitReviewTip: function() {
    this.setData({
      tip: 'wait_review'
    })
  },
  closeWaitTip: function() {
    this.setData({
      tip: 'blank'
    })
  },
  getReviewClasses: function() {
    toast.show()
    epclass.getsByReview(r => {
      if(r.code == 200) {
        for(var i = 0; i < r.data.length; i++) {
          if(i < 2) {
            this.data.reviews.push(r.data[i])
          } else {
            this.data.moreReviews.push(r.data[i])
          }
        }
        this.setData({
          reviews: this.data.reviews,
          hide_more_class: false
        })
      }
      toast.close(100)
    })
  },
  getLiveClasses: function() {
    epclass.getsByLive(r => {
      if(r.code == 200) {
        for(var i = 0; i < r.data.length; i++) {
          this.data.lives.push(r.data[i])
        }
        if(this.data.lives.length > 0) {
          this.showLiveTab()
        } else {
          this.setData({
            no_live_class: false
          })
        }
        this.setData({
          lives: this.data.lives
        })
      }
    })
  },
  showReviewTab: function(e) {
    this.setData({
      tab_live: 'off',
      tab_review: 'on',
      hide_review: false,
    })
  },
  showLiveTab: function() {
    this.setData({
      tab_live: 'on',
      tab_review: 'off',
      hide_review: true,
    })
  },
  mall: function() {
    this.setData({
      tip: 'go_mall_tip'
    })
  },
  goMall: function() {
    navigate.to('/pages/mall/index')
  },
  closeTip: function() {
    this.setData({
      tip: 'blank'
    })
  },
  review: function(event) {
    App.jice.track('click', event)
    navigate.to(`/pages/epclass/review/index?cid=${event.currentTarget.dataset.cid}`)
  },
  view: function(event) {
    App.jice.track('click', event)
    navigate.to(`/pages/epclass/view/index?cid=${event.currentTarget.dataset.cid}`)
  },
  live: function(event) {
    App.jice.track('click', event)
    navigate.to(`/pages/epclass/live/index?cid=${event.currentTarget.dataset.cid}`)
  },
  wait: function() {
    App.jice.track('click', {act: 'wait'})
    this.showWaitReviewTip()
  },
  more: function() {
    for(var i = 0; i < this.data.moreReviews.length; i++) {
      this.data.reviews.push(this.data.moreReviews[i])
    }
    this.setData({
      reviews: this.data.reviews,
      hide_more_class: true
    })
  },
  pageY: null,
  onTouchStart: function(e) {
    this.pageY = e.touches[0].pageY
  },
  onTouchMove: function(e) {
    let y = e.touches[0].pageY
    if(this.pageY - y > 100) {
      if(!this.data.hide_more_class) {
        this.more()
      }
    }
  },
  onShareAppMessage: function() {
    App.jice.track('onshare', {
      shareto: 'friend',
      title: '首页',
      path: '/pages/index/index'
    })
    return {
      title: '【专家学堂-美赞臣中国】与专家对话，做A+妈妈',
      desc: '听了这些资深专家的话，你也能成为A+妈妈哟~还不快来报名？',
      path: '/pages/index/index',
      success: function() {
        epclass.shareLogger({
          class_id: 0,
          target: '课程首页',
          action: 'appMessage'
        }, r => {
        })
      }
    }
  }
})
